{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "757d55ad-379b-462c-a97e-6b6f1423532f",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/david/anaconda3/envs/peft/lib/python3.10/site-packages/huggingface_hub/file_download.py:795: FutureWarning: `resume_download` is deprecated and will be removed in version 1.0.0. Downloads always resume when possible. If you want to force a new download, use `force_download=True`.\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "from transformers import pipeline\n",
    "pipe = pipeline(task=\"sentiment-analysis\", model=\"mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "62330874-e770-43f6-9fa7-6cf6804e78ae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'label': 'POSITIVE', 'score': 0.9961802959442139}]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pipe(\"You learn things really quickly. You understand the theory class as soon as it is taught.\")\n",
    "# 这是使用model=“distilbert/distilbert-base-uncased-finetuned-sst-2-english”"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "611ea4e8-5491-45e4-a339-a6bc043cf4e3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'label': 'en', 'score': 0.9935345649719238}]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pipe(\"You learn things really quickly. You understand the theory class as soon as it is taught.\")\n",
    "# 这是使用model=“papluca/xlm-roberta-base-language-detection”"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "f399f89b-4a70-41c8-b154-f1c6a8102df7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'label': 'positive', 'score': 0.9996744394302368}]"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pipe(\"BYD's profit increases by 30%.\")\n",
    "# 这是使用model=“mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis”"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "32a35e81-8aea-43ac-bbff-ec8e67aae974",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'label': 'positive', 'score': 0.9995080232620239}]"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pipe(\"Issue 1 trillion yuan of ultra long term special treasury bond, \\\n",
    "implement the financial discount policy for equipment renewal loans,\\\n",
    "make good use of additional treasury bond funds, speed up the issuance \\\n",
    "and use of special bonds, and implement a package of fiscal increment \\\n",
    "policies... In 2024, proactive fiscal policies will increase macro-control efforts to promote the continued recovery of the economy\")\n",
    "# 这是使用model=“mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis”"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "7f2e089f-efec-4621-acb9-a09f5b47d6bf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'label': 'POSITIVE', 'score': 0.9978430271148682}]"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pipe(\"Issue 1 trillion yuan of ultra long term special treasury bond, \\\n",
    "implement the financial discount policy for equipment renewal loans,\\\n",
    "make good use of additional treasury bond funds, speed up the issuance \\\n",
    "and use of special bonds, and implement a package of fiscal increment \\\n",
    "policies... In 2024, proactive fiscal policies will increase macro-control efforts to promote the continued recovery of the economy\")\n",
    "# 这是使用model=“distilbert-base-uncased-finetuned-sst-2-english” \n",
    "# 不同的model在处理不同领域的时候，各自擅长。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "af049006-0e85-4fe8-a3a2-ae5b5251627c",
   "metadata": {},
   "outputs": [],
   "source": [
    "text_list=[\n",
    "    \"On January 22, with the approval of the Central Financial Committee, the Central Financial Office,\\\n",
    "    the China Securities Regulatory Commission, the Ministry of Finance, the Ministry of Human Resources\\\n",
    "    and Social Security, the People's Bank of China, and the State Administration of Financial Supervision \\\n",
    "    jointly issued the Implementation Plan on Promoting the Entry of Medium and Long term Funds into the Market, \\\n",
    "    adhering to the goal and problem orientation, strengthening top-level design, focusing on the current and long-term perspective, \\\n",
    "    and guiding commercial insurance, social security funds, basic pension insurance, enterprise (occupational) annuities, public funds, \\\n",
    "    etc. to further increase their entry into the market. Promote the entry of medium and long-term funds into the market and enhance the \\\n",
    "    inherent stability of the capital market. The entry of medium and long-term funds into the market helps optimize the investor structure, \\\n",
    "    guide the establishment of value investing and long-term investment concepts, reduce market volatility and speculation, thereby boosting \\\n",
    "    investor confidence and better leveraging the market's resource allocation function. The policy focuses on improving the stability of the \\\n",
    "    capital market, with sufficient stability maintenance policies and short-term policy gaps. Market activity has fluctuated, but with external \\\n",
    "    uncertainty and internal response gradually implemented, incremental funds are expected to relay, and market activity is expected to rebound again.\",\n",
    "    \"The index continued to fall in the afternoon, and the consumer electronics sector fell into adjustment\",\n",
    "    \"The index rose and fell in the morning session, and the concept of big finance strengthened across the board\",\n",
    "    \"The index opened high in the morning and rose, with over 4800 stocks rising in two markets\"\n",
    "]\n",
    "# 这是使用model=“mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis”, 和股市相关的还是使用相匹配的model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "bc1ea349-9475-4a83-9cf8-3fc2a56f4763",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'label': 'positive', 'score': 0.999508261680603},\n",
       " {'label': 'negative', 'score': 0.9972319006919861},\n",
       " {'label': 'positive', 'score': 0.9996083378791809},\n",
       " {'label': 'positive', 'score': 0.9991957545280457}]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pipe(text_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bf24defc-2f90-4f2b-ab1a-f9ead143da3b",
   "metadata": {},
   "outputs": [],
   "source": [
    "Token Classification"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "be1fe38d-2de1-49bb-9845-ea55c3378616",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "a30bb38a1b0441f7bfc5d04d197d5a02",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "model.safetensors:   0%|          | 0.00/496M [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "70a8d5f61a4e45e2ab5e2d793f65ca8b",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "tokenizer_config.json:   0%|          | 0.00/1.24k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "02dfa17a316e4dd2a1acc3cea4d0be0b",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "vocab.json:   0%|          | 0.00/808k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "f01223f0e7bc4bc9898a4bf46899fb08",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "merges.txt:   0%|          | 0.00/467k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "7538cb7f4e774670b79e5fc6b6acf4fa",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "tokenizer.json:   0%|          | 0.00/2.13M [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "1c38c61d972e4954beac3b23bb935cc2",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "special_tokens_map.json:   0%|          | 0.00/964 [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from transformers import pipeline\n",
    "\n",
    "classifier = pipeline(task=\"ner\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "4d5394cc-e62e-4558-8d3b-b691a40e42bb",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Asking to truncate to max_length but no maximum length is provided and the model has no predefined maximum length. Default to no truncation.\n"
     ]
    }
   ],
   "source": [
    "preds = classifier(\"Hugging Face is a French company based in New York City.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "ce473aca-d6e6-434f-8581-c2f0d2a0cd5e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'entity': 'B-NNP', 'score': 0.8137, 'index': 0, 'word': 'ĠH', 'start': 0, 'end': 1}, {'entity': 'B-NNP', 'score': 0.6582, 'index': 1, 'word': 'ug', 'start': 1, 'end': 3}, {'entity': 'B-NNP', 'score': 0.7329, 'index': 2, 'word': 'ging', 'start': 3, 'end': 7}, {'entity': 'B-NNO', 'score': 0.6291, 'index': 3, 'word': 'ĠFace', 'start': 8, 'end': 12}, {'entity': 'B-NNP', 'score': 0.4099, 'index': 4, 'word': 'Ġis', 'start': 13, 'end': 15}, {'entity': 'B-NNO', 'score': 0.9724, 'index': 5, 'word': 'Ġa', 'start': 16, 'end': 17}, {'entity': 'B-NNP', 'score': 0.9614, 'index': 6, 'word': 'ĠFrench', 'start': 18, 'end': 24}, {'entity': 'B-NNP', 'score': 0.9947, 'index': 7, 'word': 'Ġcompany', 'start': 25, 'end': 32}, {'entity': 'B-NNP', 'score': 0.9971, 'index': 8, 'word': 'Ġbased', 'start': 33, 'end': 38}, {'entity': 'B-PPO', 'score': 0.9983, 'index': 9, 'word': 'Ġin', 'start': 39, 'end': 41}, {'entity': 'B-NNP', 'score': 0.9959, 'index': 10, 'word': 'ĠNew', 'start': 42, 'end': 45}, {'entity': 'B-NNP', 'score': 0.9992, 'index': 11, 'word': 'ĠYork', 'start': 46, 'end': 50}, {'entity': 'B-NNP', 'score': 0.9998, 'index': 12, 'word': 'ĠCity', 'start': 51, 'end': 55}, {'entity': 'B-SYM', 'score': 0.9956, 'index': 13, 'word': '.', 'start': 55, 'end': 56}]\n"
     ]
    }
   ],
   "source": [
    "print(preds)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "ebb443fc-08ec-464b-89fe-ad23303b63ba",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'entity': 'B-NNP', 'score': 0.8137, 'index': 0, 'word': 'ĠH', 'start': 0, 'end': 1}\n",
      "{'entity': 'B-NNP', 'score': 0.6582, 'index': 1, 'word': 'ug', 'start': 1, 'end': 3}\n",
      "{'entity': 'B-NNP', 'score': 0.7329, 'index': 2, 'word': 'ging', 'start': 3, 'end': 7}\n",
      "{'entity': 'B-NNO', 'score': 0.6291, 'index': 3, 'word': 'ĠFace', 'start': 8, 'end': 12}\n",
      "{'entity': 'B-NNP', 'score': 0.4099, 'index': 4, 'word': 'Ġis', 'start': 13, 'end': 15}\n",
      "{'entity': 'B-NNO', 'score': 0.9724, 'index': 5, 'word': 'Ġa', 'start': 16, 'end': 17}\n",
      "{'entity': 'B-NNP', 'score': 0.9614, 'index': 6, 'word': 'ĠFrench', 'start': 18, 'end': 24}\n",
      "{'entity': 'B-NNP', 'score': 0.9947, 'index': 7, 'word': 'Ġcompany', 'start': 25, 'end': 32}\n",
      "{'entity': 'B-NNP', 'score': 0.9971, 'index': 8, 'word': 'Ġbased', 'start': 33, 'end': 38}\n",
      "{'entity': 'B-PPO', 'score': 0.9983, 'index': 9, 'word': 'Ġin', 'start': 39, 'end': 41}\n",
      "{'entity': 'B-NNP', 'score': 0.9959, 'index': 10, 'word': 'ĠNew', 'start': 42, 'end': 45}\n",
      "{'entity': 'B-NNP', 'score': 0.9992, 'index': 11, 'word': 'ĠYork', 'start': 46, 'end': 50}\n",
      "{'entity': 'B-NNP', 'score': 0.9998, 'index': 12, 'word': 'ĠCity', 'start': 51, 'end': 55}\n",
      "{'entity': 'B-SYM', 'score': 0.9956, 'index': 13, 'word': '.', 'start': 55, 'end': 56}\n"
     ]
    }
   ],
   "source": [
    "preds = [\n",
    "    {\n",
    "        \"entity\": pred[\"entity\"],\n",
    "        \"score\": round(pred[\"score\"], 4),\n",
    "        \"index\": pred[\"index\"],\n",
    "        \"word\": pred[\"word\"],\n",
    "        \"start\": pred[\"start\"],\n",
    "        \"end\": pred[\"end\"],\n",
    "    }\n",
    "    for pred in preds\n",
    "]\n",
    "print(*preds, sep=\"\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "1e7ee9bc-4d7f-4f0d-a6a9-feac3ed8f06e",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "No model was supplied, defaulted to dbmdz/bert-large-cased-finetuned-conll03-english and revision f2482bf (https://huggingface.co/dbmdz/bert-large-cased-finetuned-conll03-english).\n",
      "Using a pipeline without specifying a model name and revision in production is not recommended.\n",
      "Some weights of the model checkpoint at dbmdz/bert-large-cased-finetuned-conll03-english were not used when initializing BertForTokenClassification: ['bert.pooler.dense.bias', 'bert.pooler.dense.weight']\n",
      "- This IS expected if you are initializing BertForTokenClassification from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).\n",
      "- This IS NOT expected if you are initializing BertForTokenClassification from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[{'entity_group': 'LOC',\n",
       "  'score': 0.99980444,\n",
       "  'word': 'China',\n",
       "  'start': 0,\n",
       "  'end': 5}]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "classifier = pipeline(task=\"ner\", grouped_entities=True)\n",
    "classifier(\"China's new measures at the beginning of the new year boost market confidence. \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "fec3555d-ad52-4e50-a754-a48a60291e08",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "bbee43e4bcd94bda948b7149c6f912bb",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "config.json:   0%|          | 0.00/571 [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "20a2383f126d4b96ac4ca54649621e56",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "model.safetensors:   0%|          | 0.00/496M [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "529e7d1bea2a46d88e49e37008c9362c",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "tokenizer_config.json:   0%|          | 0.00/79.0 [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "dc77a846af0f4c5d8e2a5f5fcbbef253",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "vocab.json:   0%|          | 0.00/899k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "8e3c1d3af2934441977eec6ded6bbda4",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "merges.txt:   0%|          | 0.00/456k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "12bb85837721450cb1691483d33e334b",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "special_tokens_map.json:   0%|          | 0.00/772 [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from transformers import pipeline\n",
    "\n",
    "question_answerer = pipeline(task=\"question-answering\", model=\"deepset/roberta-base-squad2\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "75c7a8a8-1fb1-4ba8-87a5-c5d9e39fafc4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "score: 0.9068, start: 30, end: 54, answer: huggingface/transformers\n"
     ]
    }
   ],
   "source": [
    "preds = question_answerer(\n",
    "    question=\"What is the name of the repository?\",\n",
    "    context=\"The name of the repository is huggingface/transformers\",\n",
    ")\n",
    "print(\n",
    "    f\"score: {round(preds['score'], 4)}, start: {preds['start']}, end: {preds['end']}, answer: {preds['answer']}\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "6a4b94b2-7479-4ebd-b52c-09daa1b7d2fd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "score: 0.1352, start: 321, end: 328, answer: Wu Qing\n"
     ]
    }
   ],
   "source": [
    "preds = question_answerer(\n",
    "    question=\"Who is speech?\",\n",
    "    context=\"\"\"At present, the China Securities Regulatory Commission is further strengthening research, \n",
    "    listening to opinions and suggestions from various aspects including industry institutions, investors,\n",
    "    etc. on the reform plan, and will also work hard to revise and improve it, and implement it as soon as possible, 'said Wu Qing.',\"\"\"\n",
    ")\n",
    "print(\n",
    "    f\"score: {round(preds['score'], 4)}, start: {preds['start']}, end: {preds['end']}, answer: {preds['answer']}\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "36a12054-99b5-4c76-9063-3a8f392f7d87",
   "metadata": {},
   "outputs": [],
   "source": [
    "from transformers import pipeline\n",
    "\n",
    "summarizer = pipeline(task=\"summarization\",\n",
    "                      model=\"facebook/bart-large-cnn\",\n",
    "                      min_length=8,\n",
    "                      max_length=32,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "350812b4-b2ea-4e2a-9fe9-0d794b238708",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'summary_text': 'The Transformer is the first sequence transduction model based entirely on attention. It replaces the recurrent layers most commonly used in encoder-decoder'}]"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "summarizer(\n",
    "    \"\"\"\n",
    "    In this work, we presented the Transformer, the first sequence transduction model based entirely on attention, \n",
    "    replacing the recurrent layers most commonly used in encoder-decoder architectures with multi-headed self-attention. \n",
    "    For translation tasks, the Transformer can be trained significantly faster than architectures based on recurrent or convolutional layers. \n",
    "    On both WMT 2014 English-to-German and WMT 2014 English-to-French translation tasks, we achieve a new state of the art. \n",
    "    In the former task our best model outperforms even all previously reported ensembles.\n",
    "    \"\"\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "946f947d-2d4d-42e2-a273-266886bd8fdb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'summary_text': 'Transformers are a set of neural networks that consist of an encoder and a decoder with self-attention capabilities. Unlike earlier recurrent neural'}]"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "summarizer(\n",
    "    '''\n",
    "    Large language models (LLM) are very large deep learning models that are pre-trained on vast amounts of data. \n",
    "    The underlying transformer is a set of neural networks that consist of an encoder and a decoder with self-attention capabilities. \n",
    "    The encoder and decoder extract meanings from a sequence of text and understand the relationships between words and phrases in it.\n",
    "    Transformer LLMs are capable of unsupervised training, although a more precise explanation is that transformers perform self-learning. \n",
    "    It is through this process that transformers learn to understand basic grammar, languages, and knowledge.\n",
    "    Unlike earlier recurrent neural networks (RNN) that sequentially process inputs, transformers process entire sequences in parallel. \n",
    "    This allows the data scientists to use GPUs for training transformer-based LLMs, significantly reducing the training time.\n",
    "    '''\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "0e2e3dc8-2ada-4e3c-a14d-58b15f2be92b",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/david/anaconda3/envs/peft/lib/python3.10/site-packages/huggingface_hub/file_download.py:795: FutureWarning: `resume_download` is deprecated and will be removed in version 1.0.0. Downloads always resume when possible. If you want to force a new download, use `force_download=True`.\n",
      "  warnings.warn(\n",
      "Some weights of the model checkpoint at superb/hubert-base-superb-er were not used when initializing HubertForSequenceClassification: ['hubert.encoder.pos_conv_embed.conv.weight_g', 'hubert.encoder.pos_conv_embed.conv.weight_v']\n",
      "- This IS expected if you are initializing HubertForSequenceClassification from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).\n",
      "- This IS NOT expected if you are initializing HubertForSequenceClassification from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).\n",
      "Some weights of HubertForSequenceClassification were not initialized from the model checkpoint at superb/hubert-base-superb-er and are newly initialized: ['hubert.encoder.pos_conv_embed.conv.parametrizations.weight.original0', 'hubert.encoder.pos_conv_embed.conv.parametrizations.weight.original1']\n",
      "You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.\n"
     ]
    }
   ],
   "source": [
    "from transformers import pipeline\n",
    "\n",
    "classifier = pipeline(task=\"audio-classification\", model=\"superb/hubert-base-superb-er\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "0ba63b80-4bb5-437a-8e5a-f41d699bb5e4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'score': 0.4532, 'label': 'hap'},\n",
       " {'score': 0.3622, 'label': 'sad'},\n",
       " {'score': 0.0943, 'label': 'neu'},\n",
       " {'score': 0.0903, 'label': 'ang'}]"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "preds = classifier(\"https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac\")\n",
    "preds = [{\"score\": round(pred[\"score\"], 4), \"label\": pred[\"label\"]} for pred in preds]\n",
    "preds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "9999a492-0c8c-4916-b9bb-3aa9c516e543",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/david/anaconda3/envs/peft/lib/python3.10/site-packages/huggingface_hub/file_download.py:795: FutureWarning: `resume_download` is deprecated and will be removed in version 1.0.0. Downloads always resume when possible. If you want to force a new download, use `force_download=True`.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "4c5a8e9896954545a3b38e33dbe2c231",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "config.json:   0%|          | 0.00/1.53k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "195ea539595342838b8b2ca7cd13cf7c",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "model.safetensors:   0%|          | 0.00/1.26G [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Some weights of the model checkpoint at jonatasgrosman/wav2vec2-large-xlsr-53-english were not used when initializing Wav2Vec2ForCTC: ['wav2vec2.encoder.pos_conv_embed.conv.weight_g', 'wav2vec2.encoder.pos_conv_embed.conv.weight_v']\n",
      "- This IS expected if you are initializing Wav2Vec2ForCTC from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).\n",
      "- This IS NOT expected if you are initializing Wav2Vec2ForCTC from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).\n",
      "Some weights of Wav2Vec2ForCTC were not initialized from the model checkpoint at jonatasgrosman/wav2vec2-large-xlsr-53-english and are newly initialized: ['wav2vec2.encoder.pos_conv_embed.conv.parametrizations.weight.original0', 'wav2vec2.encoder.pos_conv_embed.conv.parametrizations.weight.original1']\n",
      "You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "37a54e209f21480d91765d343f15c77e",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "vocab.json:   0%|          | 0.00/300 [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "dffdc6c4570a4607ba11b242d12ffa5d",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "special_tokens_map.json:   0%|          | 0.00/85.0 [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "66679d353dac42f98324b5922f39d6b4",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "preprocessor_config.json:   0%|          | 0.00/262 [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Could not load the `decoder` for jonatasgrosman/wav2vec2-large-xlsr-53-english. Defaulting to raw CTC. Error: No module named 'kenlm'\n",
      "Try to install `kenlm`: `pip install kenlm\n",
      "Try to install `pyctcdecode`: `pip install pyctcdecode\n"
     ]
    }
   ],
   "source": [
    "transcriber = pipeline(task=\"automatic-speech-recognition\", model=\"jonatasgrosman/wav2vec2-large-xlsr-53-english\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "1e0e9729-6433-48fc-8e42-b0c1c4bae1da",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'text': 'i have a dreambut one daythis nation will rise up and live up the true meaning of its creed'}"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text = transcriber(\"data/audio/mlk.flac\")\n",
    "text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "e408aab8-7694-47c9-a98f-5c6693d9aee7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "99bf030ff4904ee2ba2b4b985ecc4728",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "preprocessor_config.json:   0%|          | 0.00/266 [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Could not find image processor class in the image processor config or the model config. Loading based on pattern matching with the model's feature extractor configuration. Please open a PR/issue to update `preprocessor_config.json` to use `image_processor_type` instead of `feature_extractor_type`. This warning will be removed in v4.40.\n"
     ]
    }
   ],
   "source": [
    "from transformers import pipeline\n",
    "\n",
    "classifier = pipeline(task=\"image-classification\", model=\"microsoft/resnet-50\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "b4c38096-edf7-413a-bfcf-ac5ad16ff69e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'score': 0.5874, 'label': 'lynx, catamount'}\n",
      "{'score': 0.1289, 'label': 'tabby, tabby cat'}\n",
      "{'score': 0.075, 'label': 'marmot'}\n",
      "{'score': 0.0382, 'label': 'badger'}\n",
      "{'score': 0.0131, 'label': 'Egyptian cat'}\n"
     ]
    }
   ],
   "source": [
    "preds = classifier(\n",
    "    \"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg\"\n",
    ")\n",
    "preds = [{\"score\": round(pred[\"score\"], 4), \"label\": pred[\"label\"]} for pred in preds]\n",
    "print(*preds, sep=\"\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "585363f5-9ced-4774-aa06-ff75e522b6ce",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'score': 0.9768, 'label': 'giant panda, panda, panda bear, coon bear, Ailuropoda melanoleuca'}\n",
      "{'score': 0.0088, 'label': 'indri, indris, Indri indri, Indri brevicaudatus'}\n",
      "{'score': 0.0004, 'label': 'groenendael'}\n",
      "{'score': 0.0003, 'label': 'Siberian husky'}\n",
      "{'score': 0.0002, 'label': 'malamute, malemute, Alaskan malamute'}\n"
     ]
    }
   ],
   "source": [
    "preds = classifier(\n",
    "    \"data/image/panda.jpg\"\n",
    ")\n",
    "preds = [{\"score\": round(pred[\"score\"], 4), \"label\": pred[\"label\"]} for pred in preds]\n",
    "print(*preds, sep=\"\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "0ec91128-4cbf-4253-b6ae-b85bfe51340a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "db9a0fd5723e4c58b6fb8351709bd953",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "config.json:   0%|          | 0.00/4.38k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "c35ac699c3e445edb844a47832de1410",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "model.safetensors:   0%|          | 0.00/243M [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "f0e72a79d9f34e4d99cc6b1cc7d67d38",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "model.safetensors:   0%|          | 0.00/179M [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Some weights of the model checkpoint at facebook/detr-resnet-101 were not used when initializing DetrForObjectDetection: ['model.backbone.conv_encoder.model.layer1.0.downsample.1.num_batches_tracked', 'model.backbone.conv_encoder.model.layer2.0.downsample.1.num_batches_tracked', 'model.backbone.conv_encoder.model.layer3.0.downsample.1.num_batches_tracked', 'model.backbone.conv_encoder.model.layer4.0.downsample.1.num_batches_tracked']\n",
      "- This IS expected if you are initializing DetrForObjectDetection from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).\n",
      "- This IS NOT expected if you are initializing DetrForObjectDetection from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "bf7b1a67c3e14eb49532a8e6060a4eb4",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "preprocessor_config.json:   0%|          | 0.00/255 [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from transformers import pipeline\n",
    "\n",
    "detector = pipeline(task=\"object-detection\", model=\"facebook/detr-resnet-101\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "cd3e65a7-aaf9-49a8-a8b2-e33a19a4c4ac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'score': 0.9987,\n",
       "  'label': 'dog',\n",
       "  'box': {'xmin': 281, 'ymin': 21, 'xmax': 486, 'ymax': 415}},\n",
       " {'score': 0.995,\n",
       "  'label': 'cat',\n",
       "  'box': {'xmin': 76, 'ymin': 63, 'xmax': 314, 'ymax': 373}}]"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "preds = detector(\n",
    "   \"data/image/cat_dog.jpg\"\n",
    ")\n",
    "preds = [{\"score\": round(pred[\"score\"], 4), \"label\": pred[\"label\"], \"box\": pred[\"box\"]} for pred in preds]\n",
    "preds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "574b26e1-4e41-4def-a3e0-1f6c3587c9b9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
